package com.sys.servlet;



import com.sys.entity.Concert;
import com.sys.service.ConcertService;
import com.sys.service.impl.ConcertServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@WebServlet("/cPage")
public class SysConcertServletPage extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        ConcertService pageShow=new ConcertServiceImpl();
        int pageSum=1;     //页数
        if (req.getParameter("pageSum") !=null){
            if (Integer.parseInt(req.getParameter("pageSum")) <1){
                pageSum=1;
            }else{
                pageSum=Integer.parseInt(req.getParameter("pageSum"));
            }
        }

        int pageSize=3;      //最大页面
        if (req.getParameter("pageSize") !=null){
            if (Integer.parseInt(req.getParameter("pageSize"))<3){
                pageSize=3;
            }else {
                pageSize=Integer.parseInt(req.getParameter("pageSize"));
            }
        }
        Integer pageStatus = (pageSum-1)*pageSize;   //第几页
        Integer pageCommon = pageShow.selectCount();     //信息总数
        Integer pageTotal =  (int)Math.ceil((double)pageCommon / pageSize);    //共几页

        List<Concert> concertList=pageShow.selectAll(pageStatus,pageSize);
        if (concertList==null){
            pageSum=1;
        }
        req.setAttribute("concertList",concertList);
        req.setAttribute("pageSum",pageSum);
        req.setAttribute("pageCommon",pageCommon);
        req.setAttribute("pageTotal",pageTotal);
        req.getRequestDispatcher("CPage.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
